/* 
 * File:   main.c
 * Author: hp
 *
 * Created on 2011年1月25日, 上午2:53
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <regex.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "info.h"






extern char file_path[200];

extern void run(void * connfd);

/*
 * 
 */
int main(int argc, char** argv) {
    int listenfd, connfd;
    struct sockaddr_in servaddr;


    int n;
    int i;
    long int size=0;

    initialize();


    listenfd=socket(AF_INET,SOCK_STREAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(serv_port);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    listen(listenfd,LISTENQ);



    while(1){
        connfd=accept(listenfd,NULL,NULL);
        pthread_t thread;
        int ret=pthread_create(&thread, NULL, run, (void*) connfd);
    }

    return (EXIT_SUCCESS);
}


void client()
{
    /*
      int sockfd;
    struct sockaddr_in servaddr;

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(SERV_PORT);
    inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
    connect(sockfd,(struct sockaddr *)& servaddr,sizeof(servaddr));
    */
}

void initialize()
{
    int i=0;
    FILE *fl=fopen("config.txt","r");
    char *token;
    if (fl!=NULL){
        char line[128];
        for(i=0;fgets(line,sizeof(line),fl)!=NULL;i++)
        {
/*
          token=strtok(line," ");
              while(token!=NULL){
                  printf("%s\n",token);
                  token=strtok(NULL," ");
              }
*/
              if(i<3)
              {
                  token=strtok(line," ");
                  token=strtok(NULL," ");
                  switch(i){
                      case 0:
                          serv_port=atoi(token);
                          break;
                      case 1:
                          strncpy(file_root,token,strlen(token)-1);
                          break;
                      case 2:
                          strncpy(index_page,token,strlen(token)-1);
                          break;
                  }

              }
              else if(i>3)
              {
                  int j=(i-4)*2;
                  token=strtok(line," ");
                  strcpy(supportTypes[j],token);
                  token=strtok(NULL," ");
                  strncpy(supportTypes[j+1],token,strlen(token)-1);
              }


        }
        fclose(fl);
    }
}